package com.ququ.syb.withdraworder.service;

import com.ququ.syb.common.pojo.WithdrawOrder;
import com.ququ.syb.withdraworder.dao.WithdrawOrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@EnableTransactionManagement
public class WithdrawOrderService {
    @Autowired
    private WithdrawOrderDao withdrawOrderDao;
    @Autowired
    private WithdrawOrderIdSequenceService withdrawOrderIdSequenceService;

    @Cacheable(value = "cache", key = "'withdraworder:'+#tradenum", unless = "#result == null")
    public WithdrawOrder getWithdrawOrder(String tradenum){
        return withdrawOrderDao.getWithdrawOrder(tradenum);
    }

    public WithdrawOrder getWithdrawOrderByOutOrderNum(String outordernum){
        return withdrawOrderDao.getWithdrawOrderByOutOrderNum(outordernum);
    }

    public List<WithdrawOrder> listWithdrawOrderByPayTradenum(String paytradenum){
        return withdrawOrderDao.listWithdrawOrderByPayTradenum(paytradenum);
    }


    public List<WithdrawOrder> listWithdrawOrder(WithdrawOrder withdrawOrder){
        return withdrawOrderDao.listWithdrawOrder(withdrawOrder);
    }


    @Transactional
    @CachePut(value = "cache", key = "'withdraworder:'+#result.tradenum", unless = "#result == null")
    public WithdrawOrder save(WithdrawOrder withdrawOrder){
        withdrawOrder.setId(withdrawOrderIdSequenceService.getNextId());
        if (withdrawOrder.getTradenum() == null)
            withdrawOrder.setTradenum(withdrawOrderIdSequenceService.getNextTradenum());
        withdrawOrder.setCreatetime(new Date());
        withdrawOrderDao.save(withdrawOrder);
        return withdrawOrderDao.getWithdrawOrder(withdrawOrder.getTradenum());
    }


    @Transactional
    @CachePut(value = "cache", key = "'withdraworder:'+#withdrawOrder.tradenum", unless = "#result == null")
    public WithdrawOrder update(WithdrawOrder withdrawOrder){
        withdrawOrderDao.update(withdrawOrder);
        return withdrawOrderDao.getWithdrawOrder(withdrawOrder.getTradenum());
    }

}
